package com.zmx.patientend.view.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.zmx.patientend.BaseActivity;
import com.zmx.patientend.R;
import com.zmx.patientend.okhttp.OkHttp3ClientManager;
import com.zmx.patientend.utils.AllUtil;
import com.zmx.patientend.view.util.MySharedPreferences;
import com.zmx.patientend.view.util.TimeCount;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ModifyPwdActivity extends BaseActivity {


    @BindView(R.id.et_password_a)
    EditText etPasswordA;
    @BindView(R.id.et_password_b)
    EditText etPasswordB;
    @BindView(R.id.et_password_c)
    EditText etPasswordC;
    @BindView(R.id.submit)
    Button submit;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_modify_pwd;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:
                    try {

                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        Toast(jsonObject.getString("msg"));
                        if (jsonObject.getInt("code" ) == 1){
                            //清空本地所有数据
                            MySharedPreferences.getInstance(mActivity).clear();//清空本地数据
                            finish();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };


    @OnClick({R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.submit:

                if (!etPasswordB.getText().toString().equals(etPasswordC.getText().toString())) {
                    Toast("两次密码不正确");
                    return;
                }
                if (etPasswordA.getText().toString().equals("") ||etPasswordB.getText().toString().equals("") || etPasswordC.getText().toString().equals("")) {
                    Toast("密码不能为空");
                    return;
                }
                upPassword();
                break;
        }
    }


    public void upPassword(){


        List<String> ls = new ArrayList<>();
        ls.add(etPasswordB.getText().toString());
        ls.add(etPasswordC.getText().toString());
        ls.add(etPasswordA.getText().toString());
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "upPassword");
        params.put("data[password]", etPasswordA.getText().toString());
        params.put("data[new_password]", etPasswordB.getText().toString());
        params.put("data[new_password_confirm]", etPasswordC.getText().toString());
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/admin/api.member/entry", params, handler, 1, 404);


    }


}
